using System;
using System.Collections.Generic;
using System.Text;
using RideMe.Core.Properties;
using System.IO;

namespace RideMe.Core
{
    public class RakeProcess : ProcessBase
    {
        /// <summary>
        /// Executes an arbitrary rake command.
        /// </summary>
        /// <param name="arguments"></param>
        public override void RunCommand(string arguments, bool redirectStandardError)
        {
            ProcessExecutor processExecutor = this.CreateCommand();

            string rakePath = Options.Instance.RubyPath + Resources.RakeBatchPath;

            // Older versions of rake don't have rake.bat, so use rake.cmd instead.
            if (!Validator.ValidFile(rakePath))
            {
                rakePath = Options.Instance.RubyPath + Resources.RakeCommandPath;
            }

            processExecutor.ExecuteCommand(rakePath, arguments, redirectStandardError);
        }

        private string _projectPath;

        /// <summary>
        /// The Path to the project.
        /// <remarks>Rake needs to run in the directory of the project.</remarks>
        /// </summary>
        public string ProjectPath
        {
            get { return _projectPath; }
            set
            {
                if (_projectPath != value)
                {
                    _projectPath = value;
                    // Rake runs against the current directory so set 
                    // this to the project path.
                    Environment.CurrentDirectory = value;
                }
            }
        }

        /// <summary>
        /// Returns statistics for the current project.
        /// <remarks>rake stats command</remarks>
        /// </summary>
        public void GetProjectStats()
        {
            RunCommand(Resources.StatsCommand, true);
        }

        public void RunMigration(bool trace)
        {
            string command = Resources.MigrationCommand;

            if (trace)
            {
                command += string.Format(" {0}", Resources.Trace);
            }

            RunCommand(command, true);
        }

        

    }
}
